package org.springblade.modules.bank.controller;

import com.alibaba.excel.util.FileUtils;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.codec.Charsets;
import org.springblade.core.boot.ctrl.BladeController;
import org.springblade.modules.bank.excel.TmVisitTaskExcel;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.List;

/**
 * 文件下载控制器 控制器
 *
 * @author Blade
 * @since 2022-07-08
 */
@RestController
@RequestMapping("/download")
@Api(value = "拜访任务", tags = "拜访任务接口")
@Slf4j
public class DownloadController extends BladeController {

    @Value("${filePath}")
    private  String filePath;

    /**
     * 导出模板
     */
    @SneakyThrows
    @GetMapping("getLoginFileByName")
    @ApiOperationSupport(order = 14)
    @ApiOperation(value = "文件下载")
    public void exportUser(HttpServletResponse response,String fileNames) {
        response.setContentType("application/octet-stream");
        response.setCharacterEncoding(Charsets.UTF_8.name());
        String fileName =  "";
        if("dhkj.zip".equals(fileNames)){
            fileName= URLEncoder.encode("电话控件.zip", Charsets.UTF_8.name());
        }else if ("czsm.zip".equals(fileNames)){
            fileName= URLEncoder.encode("操作说明.zip", Charsets.UTF_8.name());
        }else {
            fileName= URLEncoder.encode(fileNames, Charsets.UTF_8.name());
        }
        response.setHeader("Content-disposition", "attachment;filename=" + fileName);
        String path = filePath+"/staticFile/"+fileNames;
        System.out.println("=========================="+path);
        byte[] fileToByteArray = FileUtils.readFileToByteArray(new File(path));
        response.getOutputStream().write(fileToByteArray);
    }

    /**
     * 客户导入模板下载
     */
    @SneakyThrows
    @GetMapping("/getCustomerImportTemplate")
    @ApiOperationSupport(order = 14)
    @ApiOperation(value = "客户导入模板下载")
    public void getCustomerImportTemplate(HttpServletResponse response) {
        List<TmVisitTaskExcel> list = new ArrayList<>();
        response.setContentType("application/vnd.ms-excel");
        response.setCharacterEncoding(Charsets.UTF_8.name());
        String fileName = URLEncoder.encode("客户导入模板", "UTF-8");
        response.setHeader("Content-disposition", "attachment;filename=" + fileName + ".xlsx");
        String path = filePath+"/excelTemplates/customerImport.xlsx";
        log.info("=========================="+path);

        byte[] fileToByteArray = FileUtils.readFileToByteArray(new File(path));
        response.getOutputStream().write(fileToByteArray);
    }
}
